package com.example.user.customviewdemo.drawing.xfermode;

import android.content.Context;
import android.graphics.AvoidXfermode;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.example.user.customviewdemo.R;

/**
 * Created by user on 2016/5/4.
 * Paint.setXfermode()在API16以上弃用了,但任然没有可以替代的方法，所以仍可以使用
 */
public class AvoidXfermodeView extends View {
    private Paint paint;
    private Bitmap bm, flowerBm;

    public AvoidXfermodeView(Context context) {
        super(context);
    }

    public AvoidXfermodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        bm = BitmapFactory.decodeResource(getResources(), R.mipmap.dog);
        flowerBm = BitmapFactory.decodeResource(getResources(), R.mipmap.flower);

        //关闭硬件加速
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    public AvoidXfermodeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = 500;
        int height = width * bm.getHeight() / bm.getWidth();
        paint.setColor(Color.RED);
        canvas.drawBitmap(bm, null, new Rect(0, 0, width, height), paint);
        paint.setTextSize(50);

        canvas.drawText("原图", 100, 400, paint);
        canvas.save();


        /**
         * public AvoidXfermode(int opColor, int tolerance, Mode mode)
         * 第一个参数opColor：一个16进制的AARRGGBB的颜色值；  表示该有该颜色的区域
         第二个参数tolerance：表示容差，这个概念我们后面再细讲
         第三个参数mode：取值有两个Mode.TARGET和Mode.AVOID；
         这里我们先知道Mode.TARGET的意义就可以了，Mode.TARGET表示将指定的颜色替换掉
         Mode.AVOID的意思就是Mode.TARGET所选区域的取反
         */


        /**
         * 离屏绘制
         * 核心代码介于canvas.saveLayer()和 canvas.restoreToCount()函数之间
         */
        /********AvoidXfermode.Mode.TARGET,所选颜色区域*************/
        canvas.translate(510, 0);
        int layerID = canvas.saveLayer(0, 0, width, height, paint, Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(bm, null, new Rect(0, 0, width, height), paint);
        paint.setXfermode(new AvoidXfermode(Color.WHITE, 100, AvoidXfermode.Mode.TARGET));//以白色为目标色，容差为100找到对应的选区；
        // canvas.drawRect(0,0,width,height,paint);//和一个红色矩形快融合，也可以是和一个bitmap融合如下：
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.flower), null, new Rect(0, -50, width, height - 50), paint);


        /********AvoidXfermode.Mode.AVOID所选区域取反**********/
      /*  canvas.restore();
        canvas.translate(0,500);
       layerID = canvas.saveLayer(0,0,width,height,paint,Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(bm,null,new Rect(0,0,width,height),paint);
        paint.setXfermode(new AvoidXfermode(Color.WHITE,100, AvoidXfermode.Mode.AVOID));
        //paint.setColor(Color.RED);
        canvas.drawRect(0,0,width,height,paint);
        canvas.restoreToCount(layerID);*/


    }
}
